package game;

public class OutList {
	// a singly linked circle list of outNode
	private int numberOfNode;
	// myNode will be the server player's node
	// it would be the first node of our iteration
	// not that myNode will ba a dummy node somehow, because if the server's
	// player need to play a card, the Server could communicate with the
	// ServerModel about this immediately
	OutNode myNode;
	// lastNode would be the one just before myNode
	OutNode lastNode;

	public int size() {
		return numberOfNode;
	}

	public OutList(OutNode myNode) {
		numberOfNode = 1;
		this.myNode = myNode;
		myNode.setNext(myNode);
		// TODO, why do i set the lastNode to be myNode???? seems wrong to me
		// now.
		lastNode = myNode;
	}

	public OutNode getMyNode() {
		return myNode;
	}

	public void add(OutNode toBadded) {
		if (myNode == null && lastNode == null) {
			myNode = toBadded;
			lastNode = toBadded;
			myNode.setNext(myNode);
			return;
		} else {
			lastNode.setNext(toBadded);
			lastNode = toBadded;
			lastNode.setNext(myNode);
//			System.out.println("After the first add:");
//			System.out.println("myNode is: " + myNode.userName);
//			System.out.println("myNode.next is:"+ myNode.next.userName);
//			System.out.println("last node is: " + lastNode.userName);
//			System.out.println("lastNode.next is:"+ lastNode.next.userName);
		}
		numberOfNode++;
	}
	
	/**
	 * this is never supposed to remove myNode
	 * @param toBremoved
	 */
	public void remove(String userName) {
		OutIterator iter = iterator();
		iter.next();
		while (iter.hasNext() && iter.getCurrent() != myNode) {
			if (iter.getCurrent().getUserName().equalsIgnoreCase(userName)) {
				System.out.println("found");
				iter.remove();
				break;
			} else {
				iter.next();
			}
		}
		numberOfNode--;
	}

	/**
	 * this is never supposed to remove myNode
	 * @param toBremoved
	 */
	public void remove(OutNode toBremoved) {
		OutIterator iter = iterator();
		iter.next();
		while (iter.hasNext() && iter.getCurrent() != myNode) {
			if (iter.getCurrent() == toBremoved) {
				iter.remove();
				break;
			}else{
				iter.next();
			}
		}
		numberOfNode--;
	}

	public void clear() {
		myNode = null;
		lastNode = null;
		numberOfNode = 0;
	}

	public OutIterator iterator() {
		OutIterator toBreturn = iterator(myNode);
		toBreturn.last = lastNode;
		return toBreturn;
	}

	// returns a iterator that is currently pointing at target node
	// IMPORTANT this method will be used when we NanMan, because of the
	// sequence.
	public OutIterator iterator(OutNode outNode) {
		return new OutIterator(outNode);
	}

	public boolean contains(String userName) {
		OutIterator iter = this.iterator();
		String first_name = iter.getCurrent().getUserName();
		do {
			String currentName = iter.getCurrent().getUserName();
			if (userName.equalsIgnoreCase(currentName)) {
				return true;
			}
			iter.next();
		} while (iter.getCurrent().getUserName() != first_name);
		return false;
	}
	/**
	 * This is only for testing purpose
	 */
	@Override
	public String toString(){
		String toBreturn ="";
		OutIterator iter = this.iterator();
		toBreturn += iter.getCurrent().userName;
		iter.next();
		while(iter.getCurrent()!= myNode){
			toBreturn += ";"+iter.getCurrent().userName;
			iter.next();
		}
		return toBreturn;
	}
}